package com.atguigu01.string.exer3;

import java.util.Scanner;

/**
 * ClassName: UserTest
 * Package: com.atguigu01.string.exer3
 * Description:
 *
 * @Author ZY
 * @Create 19:00
 * @Version 1.0
 */
public class UserTest {
    public static void main(String[] args) {
        //1.创建数组，并初始化User对象
        User[] users = new User[3];
        users[0] = new User("Tom", "1234");
        users[1] = new User("SongHK", "5678");
        users[2] = new User("Jerry", "8888");

        System.out.println("库中的用户有：");
        for (int i = 0; i < users.length; i++) {
            System.out.println(users[i]);
        }

        //2.实例化Scanner，获取输入的用户名和密码
        Scanner input = new Scanner(System.in);
        System.out.print("请输入用户名： ");
        String name = input.next();
        System.out.print("请输入密码： ");
        String password = input.next();

        //3.遍历数组，判断用户名与密码是否匹配
        boolean isFlag = true;
        for (int i = 0; i < users.length; i++) {
            if (users[i].getName().equals(name)) {
                isFlag = false;
                if (users[i].getPassword().equals(password)){
                    System.out.println("登录成功,"+name);
                    break;
                }else{
                    System.out.println("密码错误");
                }
                break;
            }
        }
        if(isFlag){
            System.out.println("用户不存在");
        }


        input.close();
    }
}
